package com.jwdf.uda.ui.fragment.home;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.VoteDelEvent;
import com.jwdf.uda.ui.activity.GuideDetailActivity;
import com.jwdf.uda.ui.activity.H5Activity;
import com.jwdf.uda.view.banner.AutoFlingPagerAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by Administrator on 2016/5/4.
 */
public class BannerAdapter extends AutoFlingPagerAdapter<PbBean.PbNav> {

    @Override
    public View instantiateView(Context context) {
        ImageView convertView = new ImageView(context);
        convertView.setScaleType(ImageView.ScaleType.FIT_XY);
        return convertView;
    }

    @Override
    public void bindView(final PbBean.PbNav bean, View view, int position) {
        ImageView imageView = (ImageView) view;
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//        imageView.setAdjustViewBounds(true);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener(v.getContext(), bean);
            }
        });

        PbBean.PbImage image=bean.getImage();
//        float ratio= (float) DisplayUtils.getDeviceWidth(view.getContext())
//                / (float) image.getWidth();
//        int maxHeight=(int)((image.getHeight())*ratio);
//        imageView.setMaxWidth(DisplayUtils.getDeviceWidth(view.getContext()));
//        imageView.setMaxHeight(maxHeight);
        if (!TextUtils.isEmpty(image.getUrl())) {
            ImageLoader.getInstance().displayImage(image.getUrl(), imageView);
        }
    }

    @Override
    public String getTitle(int position) {
        return "";
    }

    private void onItemClickListener(Context context, PbBean.PbNav bean) {
        PbBean.PbSignNavType navType = bean.getType();
        switch (navType) {
            case LINK_TYPE_NONE:
                break;
            case LINK_TYPE_WEB:
                H5Activity.start(context, bean.getUrl());
                break;
            case LINK_TYPE_GUIDE:
                GuideDetailActivity.startActivity(context, bean.getId());
                break;
        }
    }

    public void onEventMainThread(VoteDelEvent event) {
        if (mData != null || event != null) {
            for (PbBean.PbNav bean: mData) {
                if(bean.getType() == PbBean.PbSignNavType.LINK_TYPE_VOTE){
                    if(bean.getId() == event.getVoteId()){
                        mData.remove(bean);
                        notifyDataSetChanged();
                        break;
                    }
                }
            }
        }
    }
}
